package com.sz.biz.logistics.core.service.impl;

import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.core.entity.PriceFreightWeightSetting;
import com.sz.biz.logistics.core.service.PriceFreightWeightSettingService;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xutao on 17/3/23.
 */
@Service("PriceFreightWeightSettingService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PriceFreightWeightSettingServiceImpl extends AbstractService implements PriceFreightWeightSettingService {

    @Override
    public int save(PriceFreightWeightSetting entry) {
        dao.save(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_PRICE_FREIGHT_WEIGHT_SETTING", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_WEIGHT_SETTING, UserActions.ADD, "添加运输重量段价格设置", dataBackup);
        return entry.getId();
    }

    @Override
    public void batchsave(List<PriceFreightWeightSetting> list) {
        dao.batchInsert(getSqlName("insertSelective"), list);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_PRICE_FREIGHT_WEIGHT_SETTING", list);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_WEIGHT_SETTING, UserActions.ADD, "添加运输重量段价格设置", dataBackup);

    }

    @Override
    public List<PriceFreightWeightSetting> findByQuotationId(Integer quotationId) {
        ParamData pd = new ParamData();
        pd.put("quotationId", quotationId);
        List<PriceFreightWeightSetting> list = dao.findForList(getSqlName("findByQuotationId"), pd, PriceFreightWeightSetting.class);
        return list;
    }

    @Override
    public List<PriceFreightWeightSetting> findByQuotationSetId(Integer quotationSetId) {
        ParamData pd = new ParamData();
        pd.put("quotationSetId", quotationSetId);
        List<PriceFreightWeightSetting> list = dao.findForList(getSqlName("findByQuotationSetId"), pd, PriceFreightWeightSetting.class);
        return list;
    }

    @Override
    public PriceFreightWeightSetting findById(Integer id) {
        PriceFreightWeightSetting entry = (PriceFreightWeightSetting) dao.findForObject(getSqlName("selectByPrimaryKey"), id);
        return entry;
    }

    @Override
    public void deletedByQuotationId(Integer quotationId) {
        dao.delete("PriceFreightWeightSettingMapper.deletedByQuotationId", quotationId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_PRICE_FREIGHT_WEIGHT_SETTING", quotationId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_WEIGHT_SETTING, UserActions.DELETE, "删除运输重量段价格设置", dataBackup);
    }

    @Override
    public void deletedByQuotationSetId(Integer quotationSetId) {
        dao.delete("PriceFreightWeightSettingMapper.deletedByQuotationSetId", quotationSetId);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_PRICE_FREIGHT_WEIGHT_SETTING", quotationSetId);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ,
                ModuleNames.BIZ_FREIGHT_WEIGHT_SETTING, UserActions.DELETE, "删除运输重量段价格设置", dataBackup);
    }

    @Override
    protected String getMapperNamespace() {
        return "PriceFreightWeightSettingMapper";
    }
}
